const {createSchool, delSchool, updateSchool, getSchoolList, getSchoolOne} = require('../service/school')
const {SuccessModel, ErrorModel} = require('../model/ResModel')
const {addRoleFailInfo} = require('../model/ErrorInfo')
const {flatData} = require('../utils/flatData')

const getSchool = async ({name = '', pageIndex = 0, pageSize = 10, createTime = [], area_id = []}) => {
  if (typeof area_id === 'string') {
    area_id = [ area_id ]
  }
  const result = await getSchoolList({name, pageIndex, pageSize, createTime, area_id})
  result.data = flatData(result.data)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getSchoolById = async (id) => {
  let result = await getSchoolOne(+id)
  result = flatData(result)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const postSchool = async ({name, areaId}) => {
  const result = await createSchool({name, areaId})
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const patchSchool = async ({name, areaId, id}) => {
  const result = await updateSchool({name, areaId, id})
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const deleteSchool = async (id) => {
  const result = await delSchool(id)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

module.exports = {
  getSchool,
  postSchool,
  patchSchool,
  deleteSchool,
  getSchoolById
}
